home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / gettime.c < prev    next >
C/C++ Source or Header  |  1992-02-21  |  2KB  |  73 lines

  1. /*  $Revision: 1.4 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include "configdata.h"
  7. #if    defined(DO_NEED_TIME)
  8. #include <time.h>
  9. #endif    /* defined(DO_NEED_TIME) */
  10. #include <sys/time.h>
  11. #include "clibrary.h"
  12. #include "libinn.h"
  13.  
  14. int
  15. GetTimeInfo(Now)
  16.     TIMEINFO        *Now;
  17. {
  18.     static time_t    LastTime;
  19.     static long        LastTzone;
  20.     struct tm        *tm;
  21. #if    defined(DO_HAVE_GETTIMEOFDAY)
  22.     struct timeval    tv;
  23. #endif    /* defined(DO_HAVE_GETTIMEOFDAY) */
  24. #if    defined(DONT_HAVE_TM_GMTOFF)
  25.     struct tm        local;
  26.     struct tm        gmt;
  27. #endif    /* !defined(DONT_HAVE_TM_GMTOFF) */
  28.  
  29.     /* Get the basic time. */
  30. #if    defined(DO_HAVE_GETTIMEOFDAY)
  31.     if (gettimeofday(&tv, (struct timezone *)NULL) == -1)
  32.     return -1;
  33.     Now->time = tv.tv_sec;
  34.     Now->usec = tv.tv_usec;
  35. #else
  36.     /* Can't check for -1 since that might be a time, I guess. */
  37.     (void)time(&Now->time);
  38.     Now->usec = 0;
  39. #endif    /* defined(DO_HAVE_GETTIMEOFDAY) */
  40.  
  41.     /* Now get the timezone if it's been an hour since the last time. */
  42.     if (Now->time - LastTime > 60 * 60) {
  43.     LastTime = Now->time;
  44.     if ((tm = localtime(&Now->time)) == NULL)
  45.         return -1;
  46. #if    defined(DONT_HAVE_TM_GMTOFF)
  47.     /* To get the timezone, compare localtime with GMT. */
  48.     local = *tm;
  49.     if ((tm = gmtime(&Now->time)) == NULL)
  50.         return -1;
  51.     gmt = *tm;
  52.  
  53.     /* Assume we are never more than 24 hours away. */
  54.     LastTzone = gmt.tm_yday - local.tm_yday;
  55.     if (LastTzone > 1)
  56.         LastTzone = -24;
  57.     else if (LastTzone < -1)
  58.         LastTzone = 24;
  59.     else
  60.         LastTzone *= 24;
  61.  
  62.     /* Scale in the hours and minutes; ignore seconds. */
  63.     LastTzone += gmt.tm_hour - local.tm_hour;
  64.     LastTzone *= 60;
  65.     LastTzone += gmt.tm_min - local.tm_min;
  66. #else
  67.     LastTzone =  (0 - tm->tm_gmtoff) / 60;
  68. #endif    /* defined(DONT_HAVE_TM_GMTOFF) */
  69.     }
  70.     Now->tzone = LastTzone;
  71.     return 0;
  72. }
  73.